package com.jeeplus.modules.api.wxpay;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.net.ssl.SSLContext;
import java.io.File;
import java.io.FileInputStream;
import java.math.BigDecimal;
import java.security.KeyStore;

/** 
 * @Author: ws
 * @Description: 微信退款 
 * @Date: 2017-9-12 13:15 
 */ 
public class WeixinRefund {

	private static final String mchId = "1537918971";
	private static final String certPath = "E:\\付费咨询\\1537918971_20210123_cert";

	/** 
     * @Author: HONGLINCHEN 
     * @Description:微信退款方法封装   注意：：微信金额的单位是分 所以这里要X100 转成int是因为 退款的时候不能有小数点 
     * @param merchantNumber 商户这边的订单号 
     * @param wxTransactionNumber 微信那边的交易单号 
     * @param totalFee 订单总金额 
     * @param refundFee 退款金额
     * @Date: 2017-9-12 11:18
     */  
	public static JSONObject setUrl(String merchantNumber, String wxTransactionNumber, double totalFee,
			double refundFee) {
		try {
			KeyStore keyStore = KeyStore.getInstance("PKCS12");
			/*
			 * FileInputStream instream = new FileInputStream(new File(
			 * "C:\\Program Files\\微信商户平台支付证书\\apiclient_cert.p12"));
			 */
			FileInputStream instream = new FileInputStream(new File(certPath));
			try {
//				keyStore.load(instream, Global.getConfig("wx.mchId").toCharArray());
				keyStore.load(instream, mchId.toCharArray());
			} finally {
				instream.close();
			}
			SSLContext sslcontext = SSLContexts.custom()
					.loadKeyMaterial(keyStore, mchId.toCharArray()).build();
			SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" },
					null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
			CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
			HttpPost httppost = new HttpPost("https://api.mch.weixin.qq.com/secapi/pay/refund");
			// 总金额
			String totalMoney = Integer
					.toString(((new BigDecimal(Double.toString(totalFee)).multiply(new BigDecimal("100"))).intValue()));
			// 退款金额
			String refundMoney = Integer.toString(
					((new BigDecimal(Double.toString(refundFee)).multiply(new BigDecimal("100"))).intValue()));
			String xml = WXPayUtil.wxPayRefund(merchantNumber, wxTransactionNumber, totalMoney, refundMoney);

			try {
				StringEntity se = new StringEntity(xml);
				httppost.setEntity(se);
				System.out.println("executing request:" + httppost.getRequestLine());
				CloseableHttpResponse responseEntry = httpclient.execute(httppost);
				try {
					HttpEntity entity = responseEntry.getEntity();
					System.out.println("aaa:" + responseEntry.getStatusLine());
					if (entity != null) {
						System.out.println("Response content length: " + entity.getContentLength());
						SAXReader saxReader = new SAXReader();
						Document document = saxReader.read(entity.getContent());
						Element rootElt = document.getRootElement();
						System.out.println("根节点：" + rootElt.getName());
						System.out.println("result_code===" + rootElt.elementText("result_code"));
						System.out.println("return_msg===" + rootElt.elementText("return_msg"));
						String resultCode = rootElt.elementText("result_code");
						JSONObject result = new JSONObject();
						Document documentXml = DocumentHelper.parseText(xml);
						Element rootEltXml = documentXml.getRootElement();
						if (resultCode.equals("SUCCESS")) {
							System.out.println("=================prepay_id===================="
									+ rootElt.elementText("prepay_id"));
							System.out.println(
									"=================sign====================" + rootEltXml.elementText("sign"));
							result.put("weixinPayUrl", rootElt.elementText("code_url"));
							result.put("prepayId", rootElt.elementText("prepay_id"));
							result.put("status", "success");
							result.put("msg", "success");
						} else {
							result.put("status", "false");
							result.put("msg", rootElt.elementText("err_code_des"));
						}
						System.out.println("result============================:" + result);
						return result;
					}
					EntityUtils.consume(entity);
				} finally {
					responseEntry.close();
				}
			} finally {
				httpclient.close();
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			JSONObject result = new JSONObject();
			result.put("status", "error");
			result.put("msg", e.getMessage());
			return result;
		}
	}  
}
